package com.whately.core.base.swagger;

import cn.hutool.core.collection.CollUtil;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.service.ApiListing;
import springfox.documentation.service.ResourceGroup;
import springfox.documentation.service.Tag;
import springfox.documentation.service.Tags;
import springfox.documentation.spring.web.plugins.DocumentationPluginsManager;
import springfox.documentation.spring.web.scanners.ApiDescriptionReader;
import springfox.documentation.spring.web.scanners.ApiListingScanner;
import springfox.documentation.spring.web.scanners.ApiListingScanningContext;
import springfox.documentation.spring.web.scanners.ApiModelReader;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import static springfox.documentation.builders.BuilderDefaults.nullToEmptyMultimap;

@Component
@Primary
@ConditionalOnBean(springfox.documentation.spring.web.scanners.ApiDescriptionReader.class)
public class SwaggerCustomApiListingScanner extends ApiListingScanner {

    @Autowired
    public SwaggerCustomApiListingScanner(
            ApiDescriptionReader apiDescriptionReader,
            ApiModelReader apiModelReader,
            DocumentationPluginsManager pluginsManager) {

        super(apiDescriptionReader,apiModelReader,pluginsManager);
    }

    @Override
    public Multimap<String, ApiListing> scan(ApiListingScanningContext context) {

        Multimap<String, ApiListing> apiListingMap = super.scan(context);

        Map<String,Integer> groupOrderMap = Maps.newHashMap();
        Set<ResourceGroup> groupSet = context.getRequestMappingsByResourceGroup().keySet();
        for(ResourceGroup group:groupSet){
            Class c= group.getControllerClass().get();
            ApiSort apiSort = (ApiSort)c.getAnnotation(ApiSort.class);
            if(apiSort!=null){
                groupOrderMap.put(group.getGroupName(),apiSort.value());
            }
        }


        Iterable<ApiListing> allListings = Iterables.concat(nullToEmptyMultimap(apiListingMap).asMap().values());
        Iterator<ApiListing> it = allListings.iterator();
        while (it.hasNext()){
            ApiListing apiList = it.next();
            int position = Integer.MAX_VALUE;
            if(apiList.getTags()!=null){

                if(CollUtil.isNotEmpty(apiList.getApis())){
                    String groupName = apiList.getApis().get(0).getGroupName().get();
                    if(groupName!=null&&groupOrderMap.containsKey(groupName)){
                        position = groupOrderMap.get(groupName);
                    }
                }
                TreeSet<Tag> tagSet = Sets.newTreeSet(Tags.tagComparator());

                for(Tag tag:apiList.getTags()){
                    Tag newTag = new Tag(tag.getName(),tag.getDescription(),position,tag.getVendorExtensions());
                    tagSet.add(newTag);
                }
                //清除旧的
                apiList.getTags().clear();
                //添加新的
                apiList.getTags().addAll(tagSet);
            }
        }

        return apiListingMap;
    }



}
